<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class Competitions extends CI_Controller {

    public function __construct() {
        parent::__construct();
        $this->lang->load('breadcrumb', 'estonian');
        $this->load->model('Competition_model', 'CompetitionModel');
    }

    public function index($date = false) {
        $max_year = $this->CompetitionModel->getMaxDate();
        $prev_year = $max_year - 1;
        if ($date == false) {
            redirect("competitions/$prev_year-$max_year");
        } else if (is_numeric($date) && $date >= 2000 && $date <= 2050) {
            $data['competitions'] = $this->CompetitionModel->fetchByYear($date, true);
        } else {
            $splits = explode("-", $date);
            if (!isset($splits[0]) || !is_numeric($splits[0])) {
                $min = $prev_year;
            } else {
                $min = $splits[0];
            }
            if (!isset($splits[1]) || !is_numeric($splits[1])) {
                $max = $max_year;
            } else {
                $max = $splits[1];
            }
            if ($min <= 1950 || $min >= 2050) {
                $min = $prev_year;
            }
            if ($max <= 1950 || $max >= 2050) {
                $max = $max_year;
            }
            $data['competitions'] = $this->CompetitionModel->fetchBySeason($min, $max, true);
        }

        $data['min_date'] = $this->CompetitionModel->getDate('min');
        $data['max_date'] = $max_year;
        $this->load->template('competitions', $data);
    }

    public function summary($id = false, $context = false) {
        if ($id == false || $this->CompetitionModel->count_results($id) == 0) {
            redirect('competitions');
        } else {
            $best['min_year'] = $this->CompetitionModel->getAthleteYear($id, 'min');
            $best['max_year'] = $this->CompetitionModel->getAthleteYear($id, 'max');
            if ($this->input->post('ajax')) {
                $min_year = $this->input->post('min_year');
                if (!is_numeric($min_year) || !($min_year >= $best['min_year'] && $min_year <= $best['max_year'])) {
                    $min_year = $best['min_year'];
                }
                $max_year = $this->input->post('max_year');
                if (!is_numeric($max_year) || !($max_year >= $best['min_year'] && $max_year <= $best['max_year'])) {
                    $max_year = $best['max_year'];
                }
            } else {
                $min_year = $best['min_year'];
                $max_year = $best['max_year'];
            }

            $best['sel_min_year'] = $min_year;
            $best['sel_max_year'] = $max_year;

            $best['best_results_m'] = $this->CompetitionModel->fetch_top_results($id, 'M', $min_year, $max_year);
            $best['best_results_f'] = $this->CompetitionModel->fetch_top_results($id, 'F', $min_year, $max_year);

            if ($this->input->post('ajax')) {
                $this->load->view('competition_best', $best);
            } else {
                $context = $context == 'tuk' ? 'tuk' : false;
                $data = $this->CompetitionModel->fetchEventsResults($id, $context);
                $data['short_events'] = $this->CompetitionModel->fetch_short_events($id);

                $data['num_athletes'] = $this->CompetitionModel->count_athletes($id, $context);
                $data['num_teams'] = $this->CompetitionModel->count_teams($id);
                $data['competition'] = $this->CompetitionModel->fetch($id);

                $data['competition_best'] = $this->load->view('competition_best', $best, true);
                $this->load->template('competition', $data);
            }
        }
    }

    public function ajax_best_fina() {


        if (!is_numeric($min_year) || !is_numeric($max_year)) {
            die();
        }

        $best['best_results_m'] = $this->CompetitionModel->fetch_top_results($id, 'M', $min_year, $max_year);
        $best['best_results_f'] = $this->CompetitionModel->fetch_top_results($id, 'F', $min_year, $max_year);

//        $this->load->view('competition_best', $best);
    }

}